import Vue from 'vue'
import VueRouter from 'vue-router'
import {
	getToken
} from '@/utils/auth'
// import Utils from '../utils/util.js'

Vue.use(VueRouter)

const routes = [{
		path: '/',
		redirect: '/index',
		meta: {
			title: '',
		},
	},
	{
		path: '/login',
		name: 'Login',
		meta: {
			title: '',
		},
		component: () => import('@/views/login')
	},
	{
		path: '/index',
		name: 'Index',
		meta: {
			title: '',
		},
		component: () => import('@/views/index')
	},
	{
		path: '/addInr',
		name: 'AddInr',
		meta: {
			title: '',
		},
		component: () => import('@/views/addInr')
	},
	{
		path: '/sell',
		name: 'Sell',
		meta: {
			title: '',
		},
		component: () => import('@/views/sell')
	},
	{
		path: '/details',
		name: 'Details',
		meta: {
			title: '',
		},
		component: () => import('@/views/details')
	},
	{
		path: '/customer',
		name: 'Customer',
		meta: {
			title: '',
		},
		component: () => import('@/views/customer')
	},
	{
		path: '/selectSell',
		name: 'SelectSell',
		meta: {
			title: '',
		},
		component: () => import('@/views/selectSell')
	},
	{
		path: '/assign',
		name: 'Assign',
		meta: {
			title: '',
		},
		component: () => import('@/views/assign')
	},
	{
		path: '/assignStaff',
		name: 'AssignStaff',
		meta: {
			title: '',
		},
		component: () => import('@/views/assignStaff')
	},
	{
		path: '/check',
		name: 'Check',
		meta: {
			title: '',
		},
		component: () => import('@/views/check')
	},
	{
		path: '/forms',
		name: 'Forms',
		meta: {
			title: '',
		},
		component: () => import('@/views/forms')
	},
	{
		path: '/formsInfo',
		name: 'FormsInfo',
		meta: {
			title: '',
		},
		component: () => import('@/views/formsInfo')
	},
	{
		path: '/mySell',
		name: 'MySell',
		meta: {
			title: '',
		},
		component: () => import('@/views/mySell')
	},
	{
		path: '/mySellDetails',
		name: 'MySellDetails',
		meta: {
			title: '',
		},
		component: () => import('@/views/mySellDetails')
	}
]


const router = new VueRouter({
	mode: 'hash', // history
	scrollBehavior: () => ({
		y: 0
	}),
	base: process.env.BASE_URL,
	routes
})

router.beforeEach((to, from, next) => {
	if(to.name == 'Login' && to.query.id) return next() // 测试入口
	const userinfo = JSON.parse(localStorage.getItem('userinfo'))
	let token = getToken();
	// 判断路由是否需要登录权限
	if (!token && to.name != 'Login') {
		next({
			path: `/login`
		});
	} else {
		if(userinfo && userinfo.type === 3 && to.name != 'FormsInfo') return next('formsInfo')
		next();
	}
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

export default router
